package com.java.code.jdbc.interceptor;

import com.java.code.domain.Page;
import com.java.code.domain.Todo;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.Properties;

//        插件的功能
//        Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
//        ParameterHandler (getParameterObject, setParameters)
//        ResultSetHandler (handleResultSets, handleOutputParameters)
//        StatementHandler (prepare, parameterize, batch, update, query)
@Intercepts({@Signature(
        type = StatementHandler.class,
        method = "prepare",
        args = {Connection.class, Integer.class})
})
public class PageInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = MetaObject.forObject(statementHandler,
                SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
                new DefaultReflectorFactory());
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        String id = mappedStatement.getId();
        if (id.endsWith("Page")) {
            BoundSql boundSql = statementHandler.getBoundSql();
            Map<String, Object> map = (Map<String, Object>) boundSql.getParameterObject();
            //需要在具体查询方法Page参数前添加@Param("page")
            Page<Todo> page = (Page<Todo>) map.get("page");
            String sql = boundSql.getSql();
            String countSql = "select count(*) from (" + sql + ") a";
            Connection connection = (Connection) invocation.getArgs()[0];
            PreparedStatement preparedStatement = connection.prepareStatement(countSql);
            ParameterHandler parameterHandler = (ParameterHandler) metaObject.getValue("delegate.parameterHandler");
            parameterHandler.setParameters(preparedStatement);
            ResultSet rs = preparedStatement.executeQuery();
            if (rs.next()) {
                page.setTotalPage(rs.getInt(1));
            }
            String pageSql = sql + " limit " + (page.getIndexPage() - 1) * page.getPageSize() + ", " + page.getPageSize();
            metaObject.setValue("delegate.boundSql.sql", pageSql);
        }

        // implement pre processing if need
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
